/* 
 * Shuriken is the legal property of its developers. Please refer to the
 * COPYRIGHT file distributed with this
 * source distribution.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using Shuriken.Core.Utils;
using System.Runtime.InteropServices;
using Shuriken.Base; 

namespace Shuriken.Core.Store.Scanners
{
    public class FileSystemScanner : IScanner
    {
        private FileScannerHelper scanner;
        private List<ISubject> m_ItemList; 

        public FileSystemScanner(string[] locations, int level, string[] patterns) 
        {
            scanner = new FileScannerHelper(locations, level, patterns);
            m_ItemList = new List<ISubject>();
        }

        /// <summary>
        /// Creates a store item 
        /// </summary>
        /// <param name="link">item to add to the store</param>
        /// <param name="ShellImageIndex">index of the icon for this icon</param>
        /// <returns>store item</returns>
        public ISubject CreateItem(string link, int ShellImageIndex)
        {
            Shell32.FolderItem fItem = null;

            fItem = ShellUtils.GetFolderItemFromFile(link);

            Store.Shell32FolderItem shItem = new Shell32FolderItem(fItem);            
            shItem.Icon = ImageStore.AddSystemImageListIndex(ShellImageIndex);

            return shItem; 
        }

        #region IScanner Members

        public List<ISubject> Items
        {
            get { return m_ItemList; }
        }

        public void Update()
        {
            List<string> items = scanner.Scan();
            m_ItemList.Clear();

            //int index = -1;
            foreach (string link in items)
            {
                try
                {
                    Shuriken.Core.Shell.ShellAPI.SHFILEINFO shInfo = new Shuriken.Core.Shell.ShellAPI.SHFILEINFO();

                    Shuriken.Core.Shell.ShellAPI.SHGetFileInfo(link, 0, out shInfo, (uint)Marshal.SizeOf(shInfo),
                        Shuriken.Core.Shell.ShellAPI.SHGFI.SHGFI_DISPLAYNAME |
                        Shuriken.Core.Shell.ShellAPI.SHGFI.SHGFI_SMALLICON |
                        Shuriken.Core.Shell.ShellAPI.SHGFI.SHGFI_SYSICONINDEX
                    );

                    string DisplayName = shInfo.szDisplayName;
                    int IconIndex = shInfo.iIcon;

                    ISubject item = CreateItem(link, IconIndex);
                    m_ItemList.Add(item); 
                }
                catch (Exception excp)
                {
                    Exception x = excp;
                    continue;
                }

            }           
        }

        #endregion

        #region IObject Members

        public string Name
        {
            get { return "File System"; }
        }

        public string Description
        {
            get { return "Scanner for scanning the file system."; }
        }

        public string Icon
        {
            get
            {
                return string.Empty;
            }
            set
            {
                
            }
        }

        #endregion
    }
}
